package com.dzx.currentency.unit2;

import com.dzx.currentency.annoations.ThreadSafe;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author DuanZhaoXu
 * @ClassName:
 * @Description:
 * @date 2018年11月17日 14:46:52
 */
@Slf4j
@ThreadSafe
public class AtomicExample3 {

    @Data
    static  class  Student {
        private  int age=0 ;
        public Student(int age) {
            this.age = age;
        }
    }

    //请求次数
    private  static  AtomicReference<Integer> count =new AtomicReference<Integer>(0);
    private static AtomicReference<Student> studentAtomic =new AtomicReference<Student>(new Student(0));

    public static void  main(String[] args){

        //原子更新integer
        count.compareAndSet(0,2);
        count.compareAndSet(0,1);
        count.compareAndSet(1,3);
        count.compareAndSet(2,4);
        count.compareAndSet(3,5);
        log.info("count:{}",count.get());

        //原子更新学生对象的age
        studentAtomic.compareAndSet(studentAtomic.get(),new Student(10));
        log.info("count:{}",studentAtomic.get().getAge());
    }
}

